﻿using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLine.Models;
using ZoneLine.Models.XWStations;
using ZoneLine.Printers;

namespace ZoneLine.Stations.XWStation
{
    /// <summary>
    /// DB55
    /// </summary>
    public class XW010RStation : XWStationBase
    {
        public XW010RStation()
        {
            _logUiChannel = nameof(XW010RStation) + "uilog";

            PrintRltAddr = "DB55.502";

            QueryCmd.Execute(null);
        }

        /// <summary>
        /// 打印结论plc地址
        /// </summary>
        public string PrintRltAddr { get; set; }

        private ObservableCollection<XW010RStationResultTemp> _recordInfos;

        public ObservableCollection<XW010RStationResultTemp> RecordInfos
        {
            get { return _recordInfos; }
            set => SetProperty(ref _recordInfos, value);
        }


        public RelayCommand<XW010RStationResultTemp> QualifiedInfoCmd => new RelayCommand<XW010RStationResultTemp>((arg) =>
        {
            if (arg.Result.ToUpper() == "N" || arg.Result.ToUpper() == "NG")
            {
                var res = HandyControl.Controls.MessageBox.Ask("确认是否将此合件变更为合格？", "温馨提示");
                if (res == System.Windows.MessageBoxResult.OK)
                {
                    var rs = _dal.UpdateModel<XW010RStationResultTemp>(o => o.Id == arg.Id, p => new XW010RStationResultTemp() { Result = "OK" });
                    if (rs) QueryCmd.Execute(null);
                }
            }
        });

        public RelayCommand QueryCmd => new RelayCommand(() =>
        {
            LogWithUi(_logUiChannel, $"在{CurrentStationName}查询完成");
            var list = _dal.GetModelList<XW010RStationResultTemp>(o => true);
            RecordInfos = new ObservableCollection<XW010RStationResultTemp>(list);
        });

        public RelayCommand PrintCmd => new RelayCommand(() =>
        {
            PrintLabel();
        });


        protected override int RecordData()
        {
            var result = new XW010RStationResultTemp()
            {
                AbsScanCode = StationRecordData.AbsScanCode,
                BushScanCode = StationRecordData.BushScanCode,
                StationCode = this.StationCode,
                PartType = StationRecordData.PartType,
                Result = StationRecordData.Result,
                PressPosition = StationRecordData.PressPosition,
                PressForce = StationRecordData.PressForce,
                CycleTime = StationRecordData.CycleTime,
                Code = string.Empty,
                //物料
                UpSupportBatchCode = StationRecordData.UpSupportBatchCode,
                UpSupportBatchCodeCut = StationRecordData.UpSupportBatchCodeCut,
                BumperBatchCode = StationRecordData.BumperBatchCode,
                BumperBatchCodeCut = StationRecordData.BumperBatchCodeCut,
                BearingBatchCode = StationRecordData.BearingBatchCode,
                BearingBatchCodeCut = StationRecordData.BearingBatchCodeCut,
                DustCoverBatchCode = StationRecordData.DustCoverBatchCode,
                DustCoverBatchCodeCut = StationRecordData.DustCoverBatchCodeCut,
                SpringPadBatchCode = StationRecordData.SpringPadBatchCode,
                SpringPadBatchCodeCut = StationRecordData.SpringPadBatchCodeCut,
            };
            var writeRlt = _dal.AddModel(result);
            return writeRlt ? 1 : 2;
        }

        protected override void PrintLabel()
        {
            try
            {
                HJYZPCPrinter printer = new HJYZPCPrinter();
                var list = _dal.GetModelList<XW010RStationResultTemp>(p => true).ToList();
                var boxQrCode = CreateComponentScanCode(list.Count);

                #region 执行打印TODO
                //打印标签信息待配置TODO
                string templatePath = "";
                string printerName = "";
                Dictionary<string, string> pairs = new Dictionary<string, string>();
                pairs.Add("QRCode", boxQrCode);

                LogWithUi(_logUiChannel, $"在{CurrentStationName}开始打印第，模板是【${templatePath}】");
                //printer.PrintLabel(templatePath, printerName, pairs);
                LogWithUi(_logUiChannel, $"在{CurrentStationName}打印完成");
                #endregion

                #region 打印完成之后需要新增到正式表中，然后清除临时表信息
                list.ForEach(o =>
                {
                    o.ComponentScanCode = boxQrCode;
                });
                var resultList = list.Cast<XW010RStationResult>().ToList();
                var res = _dal.AddModels(resultList);
                if (res)
                {
                    //清除表信息
                    _dal.DeleteModel<XW010RStationResultTemp>(o => true);
                }
                #endregion

                WriteData(PrintRltAddr, 1);

            }
            catch (Exception ex)
            {
                LogWithUi(_logUiChannel, $"在{CurrentStationName}打印失败,失败原因{ex.Message}");
                WriteData(PrintRltAddr, 2);
            }
        }
     
    }
}
